﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;

namespace NJL.WebUsers.Shop
{
    public partial class Default : BasePageVisitor
    {
        NJL.BLL.NJL_CS_SERVICE_TYPE bllServiceType = new BLL.NJL_CS_SERVICE_TYPE();
        NJL.BLL.NJL_COMPANY_SERVICE bllService = new BLL.NJL_COMPANY_SERVICE();
        NJL.BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        public NJL.Model.NJL_COMPANY modelCompany;
        NJL.Model.NJL_USER modelUser = new Model.NJL_USER();
        NJL.BLL.NJL_USER bllUser = new BLL.NJL_USER();
        NJL.BLL.NJL_PERSON bllPerson = new BLL.NJL_PERSON();
        NJL.BLL.NJL_LEAVE_MESSAGE bllMessage = new BLL.NJL_LEAVE_MESSAGE();
        NJL.Model.NJL_LEAVE_MESSAGE modelMessage = new Model.NJL_LEAVE_MESSAGE();
        NJL.BLL.NJL_ATTRACTION bllAttraction = new BLL.NJL_ATTRACTION();

        protected void Page_Load(object sender, EventArgs e)
        {
            modelCompany = bllCompany.GetModel(Request.QueryString["id"]);
            if (!IsPostBack)
            {
                bind();
            }
        }
        void bind()
        {
            //if (modelCompany.COMPANY_INTRO.Length > 300)
            //{
            //    litDescription.Text = modelCompany.COMPANY_INTRO.Substring(0, 300);
            //}
            //else
            //{
            //    litDescription.Text = modelCompany.COMPANY_INTRO;
            //}
            if (Request.QueryString["id"] == null || Request.QueryString["id"] == "")
            {
                Response.Redirect("/", true);
            }


            modelCompany = bllCompany.GetModel(Request.QueryString["id"]);
             DataSet dsService;
            if (modelCompany != null)
            {  

                #region 服务列表
                if (Request.QueryString["typeid"] != null)
                {
                    dsService = bllService.GetList("NJL_COMPANY_ID='" + modelCompany.ID + "' and NJL_COMPANY_SERVICE.TYPE_CODE='" + Request.QueryString["typeid"] + "'");
                    if (dsService.Tables[0].Rows.Count > 8)
                    {
                        pager.InnerHtml = Royalsoft.Common.Pager.GetPageNum(dsService, rptServiceList, 8, new string[] { "id" }, "/shangpu/type-"+Request.QueryString["typeid"]+"-");
                    }
                    else
                    {
                        rptServiceList.DataSource = dsService;
                        rptServiceList.DataBind();
                    }
                }
                else
                {
                  dsService = bllService.GetList("NJL_COMPANY_ID='" + modelCompany.ID + "'");
                    if (dsService.Tables[0].Rows.Count > 8)
                    {
                        pager.InnerHtml = Royalsoft.Common.Pager.GetPageNum(dsService, rptServiceList, 8, new string[] { "id" }, "/shangpu/");
                    }
                    else
                    {
                        rptServiceList.DataSource = dsService;
                        rptServiceList.DataBind();
                    }

                }
                #endregion

                #region 服务类型
                dsService = bllService.GetCompanyIdByTypeName(modelCompany.ID);
                rptService.DataSource = dsService;
                rptService.DataBind();
                #endregion
                #region 活动列表
                NJL.BLL.NJL_ACTIVE bllActive = new BLL.NJL_ACTIVE();
                dsService = bllActive.GetList(10, "IS_PUBLISH=1 and NJL_COMPANY_ID='" + Request.QueryString["ID"] + "'", "PUBLISH_DATE_TIME desc");

                if (dsService != null && dsService.Tables.Count > 0 && dsService.Tables[0].Rows.Count > 0)
                {
                    divLastActive.Style["display"] = "block";

                    if (dsService.Tables[0].Rows.Count == 1)
                    {
                        DataRow drTemp = dsService.Tables[0].NewRow();
                        for (int i = 0; i < dsService.Tables[0].Columns.Count; i++)
                        {
                            drTemp[i] = dsService.Tables[0].Rows[0][i];
                        }
                        dsService.Tables[0].Rows.Add(drTemp);
                    }
                }
                else
                {
                    divLastActive.Style["display"] = "none";
                }

                rptActive.DataSource = dsService;
                rptActive.DataBind();

                //int serviceCount = dsService.Tables[0].Rows.Count;
                //if (serviceCount <= 6)
                //{
                //    dsService = bllActive.GetList(2, "IS_PUBLISH=1 and NJL_COMPANY_ID='" + Request.QueryString["ID"] + "'", "PUBLISH_DATE_TIME desc");
                //}
                //else if (serviceCount <= 12 && serviceCount > 6)
                //{
                //    dsService = bllActive.GetList(5, "IS_PUBLISH=1 and NJL_COMPANY_ID='" + Request.QueryString["ID"] + "'", "PUBLISH_DATE_TIME desc");
                //}
                //else if (serviceCount > 12 && serviceCount <= 18)
                //{
                //    dsService = bllActive.GetList(9, "IS_PUBLISH=1 and NJL_COMPANY_ID='" + Request.QueryString["ID"] + "'", "PUBLISH_DATE_TIME desc");
                //}
                //else if (serviceCount > 18)
                //{
                //    dsService = bllActive.GetList(13, "IS_PUBLISH=1 and NJL_COMPANY_ID='" + Request.QueryString["ID"] + "'", "PUBLISH_DATE_TIME desc");
                //}
                #endregion

                //#region 留言
                //DataSet dsMessage = bllMessage.GetList(3, "NJL_USER_ID='" + Request.QueryString["id"] + "' and LEAVE_MESSAGE_TYPE_CODE=2", " CREATE_DATE_TIME DESC");
                //rptCommentList.DataSource = dsMessage;
                //rptCommentList.DataBind();
                //#endregion
                #region 游客分享
                NJL.BLL.NJL_NEWS bllNews = new BLL.NJL_NEWS();
                DataSet ds = bllNews.GetList(4, "SHARE_COMPANY_ID='" + Request.QueryString["id"] + "'", "CREATE_DATE_TIME desc");
                rptShare.DataSource = ds;
                rptShare.DataBind();
                #endregion

                #region 点击点击次数
                if (bllCompany.updateHitandDate(Request.QueryString["id"]))
                {
                }
                #endregion

                LoadPageSEO();
            }
            else
            {
                Response.Redirect("/", true);
            }
        }
        /// <summary>
        /// 加载页面SEO
        /// </summary>
        private void LoadPageSEO()
        {
            Page.Title = modelCompany.SEO_TITLE;
            HtmlMeta myMeta = new HtmlMeta();
            myMeta.Name = "Keywords";
            myMeta.Content = modelCompany.SEO_KEYWORDS;
            this.Header.Controls.Add(myMeta);
            HtmlMeta myMeta1 = new HtmlMeta();
            myMeta1.Name = "Description";
            myMeta1.Content = modelCompany.SEO_DESCRIPTION;
            this.Header.Controls.Add(myMeta1);
        }

        protected void rptServiceList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Image img = e.Item.FindControl("imgPic") as Image;
                DataRowView row = (DataRowView)e.Item.DataItem;
                string id = row["IMAGE_CODE"].ToString();
                if (!string.IsNullOrEmpty(id))
                {
                    img.ImageUrl = BasePage.GetImageUrl(id, "6");
                }
                else
                {
                    img.ImageUrl = "~/images/nopic.jpg";
                }
            }
        }
        Random random = new Random(Guid.NewGuid().GetHashCode());
        protected void rptimpression_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Literal lit = e.Item.FindControl("litCss") as Literal;
                string[] tag = { "orange02", "orange", "green", "orange", "" };
                int rand = random.Next(0, tag.Length - 1);
                lit.Text = tag[rand];
            }
        }

        //protected void btnComment_Click(object sender, EventArgs e)
        //{
        //    if (UserId == "" || UserType == "-1")
        //    {
        //        Royalsoft.Common.MessageBox.ShowAndRedirect(this, "请登录后再发表", "/Login.aspx?url=" + Server.UrlEncode(Request.RawUrl));
        //        return;
        //    }
        //    if (Session["CheckCode"] != null)
        //    {
        //        if (txtValidate.Text.Trim().ToLower() != Session["CheckCode"].ToString().ToLower())
        //        {
        //            Royalsoft.Common.MessageBox.Show(this, "请输入正确的验证码");
        //            return;
        //        }
        //        modelCompany = bllCompany.GetModel(Request.QueryString["id"].ToString());
        //        modelMessage.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
        //        modelMessage.LEAVE_MESSAGE_TYPE_CODE = 2;
        //        modelMessage.LEAVE_MESSAGE = txtComment.Text.Trim();
        //        modelMessage.NJL_USER_ID = Request.QueryString["id"].ToString();
        //        modelMessage.CREATE_USER_ID = new BasePage().UserId;
        //        modelMessage.CREATE_USER_NAME = new BasePage().UserName;
        //        modelMessage.CREATE_DATE_TIME = DateTime.Now;
        //        bllMessage.Add(modelMessage);
        //        Royalsoft.Common.MessageBox.ShowAndRedirect(this, "恭喜您留言成功", Request.RawUrl);
        //        if (bllCompany.updateCommentHitAndDate(Request.QueryString["id"]))
        //        {

        //        }

        //    }
        //    else
        //    {
        //        Royalsoft.Common.MessageBox.ShowAndRedirect(this, "连接系统超时", "" + Request.RawUrl);
        //    }

        //}
        //protected void rptCommentList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        //{
        //    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        //    {
        //        DataRowView grv = (DataRowView)e.Item.DataItem;
        //        string userId = grv["CREATE_USER_ID"].ToString();
        //        Image imgUserHead = e.Item.FindControl("imgUserHead") as Image;
        //        modelPerson = bllPerson.GetModelByUserId(userId);
        //        if (modelPerson != null)
        //        {
        //            if (modelPerson.IMAGE_CODE != "")
        //            {
        //                imgUserHead.ImageUrl = GetImageUrl(modelPerson.IMAGE_CODE, "U1");
        //                imgUserHead.BorderWidth = 1;
        //            }
        //            else
        //            {
        //                imgUserHead.ImageUrl = "/Images/avatar.jpg";
        //                imgUserHead.BorderWidth = 1;
        //            }
        //        }
        //        modelCompany = bllCompany.GetModel(Request.QueryString["id"]);
        //        LinkButton lbtn = e.Item.FindControl("lbtnDel") as LinkButton;
        //        if (UserId != "" && UserId.Equals(modelCompany.NJL_USER_ID))
        //        {
        //            lbtn.Visible = true;
        //        }
        //        else
        //        {
        //            lbtn.Visible = false;
        //        }
        //    }
        //}

        //protected void rptCommentList_ItemCommand(object source, RepeaterCommandEventArgs e)
        //{
        //    if (e.CommandName == "Del")
        //    {
        //        string id = e.CommandArgument.ToString();
        //        if (bllMessage.Delete(id))
        //        {
        //            bind();
        //        }
        //        else
        //        {
        //            Royalsoft.Common.MessageBox.Show(this, "删除失败");
        //        }
        //    }
        //}
    }
}